<?php
class LicenseAction extends AdminBaseAction {

   public function index(){
	   	import('ORG.Util.Page');       //导入分页类
		$license=M('License');
		$condition['disabled']='false';
        $count  = $license->where($condition)->count();    //计算总数
        $Page = new Page($count, C('PAGE_NUM'));
        $list   = $license->limit($Page->firstRow. ',' . $Page->listRows)->where($condition)->order('id desc')->select();
		$modifierlist=$this->modifier_customer_product($list);
        $Page->setConfig('header', '条数据');
        $Page->setConfig('first', '<<');
        $Page->setConfig('last', '>>');
        $page = $Page->show();
		$this->assign("count",$count);
        $this->assign("page", $page);
        $this->assign("list", $modifierlist);
        $this->display();
		$this->display();
	}

	 private function modifier_customer_product($list){
		 $newList=array();

		 if(empty($list)){
			 $list=array();
		 }
		 $arrayObject=new ArrayObject($list);
		 $it=$arrayObject->getIterator();
		 $customer=M('Customer');
		 $product=M('Product');
		 //这里没有效率,需要重构
		 while($it->valid()){
			$current=$it->current();
			if(is_array($current)){
				$clist=$customer->where(array('id'=>$current['customer_id']))->field('name')->select();
				$plist=$product->where(array('id'=>$current['product_id']))->field('name')->select();
				$current['customer_id']=$clist[0]['name'];
				$current['product_id']=$plist[0]['name'];
				$newList[]=$current;
			}
			$it->next();
		 }
		return $newList;
	 }
   public function showadd(){
	   $customer=M('Customer');
	   $customerList=$customer->select();
	   $product=M('Product');
	   $productList=$product->select();
	   $this->assign('customerList',$customerList);
	   $this->assign('productList',$productList);
	   $this->display();
	}

   public function add(){
		$license=M('License');
		$_POST['start_time']=strtotime($_POST['start_time']);
		$_POST['end_time']=strtotime($_POST['end_time']);
		$result=$license->add($_POST);
		if($result){
			$this->success('添加成功');
			$this->redirect('License/index');
		}else{
			$this->error('添加错误');
		}
	}
public function recycle(){
		$map['id']  = array('in',$_POST['values']);
		$license=M('License');
		$data['disabled']='true';
		$license->where($map)->save($data);
		$this->redirect('License/index');
	}
}
?>